/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 *
 * @author dianacha
 */
public class FormularioSucursal extends JDialog implements ActionListener{
    private JLabel lSucursal;
    private JTextField codSuc;
    private JTextField nombSuc;
    private JTextField dirSuc;
    private JTextField telSuc;
    private JTextField numTVen;
    private JButton bConsultarSuc;
    private JButton bCrear;
    private JButton bModificar;
    private JButton bEliminar;
    private JComboBox cAccionesBoton;
    private final boolean[] permisos;
    private ArrayList datos;
    
    public FormularioSucursal(JFrame framePadre, boolean modal, String tipoUser, boolean[] permisos){
        super(framePadre, modal);
        datos = new ArrayList();
        setTitle("Cajero Cocos - Sucursales - "+tipoUser);
        System.out.println("1 "+permisos[0]+" 2 "+permisos[1]+" 3 "+permisos[2]);
        //Image icono = Toolkit.getDefaultToolkit().getImage("images/logo25x25.jpg");
        //setIconImage(icono);
        this.permisos=permisos;
        inicializarComponentes();
        organizarComponentes();
        pack();
        setResizable(false);
        setVisible(true);
    }
    
    private void inicializarComponentes() {
        lSucursal = new JLabel("Gestión Sucursales", new ImageIcon("/GUI/images/shopping-bag-96x96.png"), SwingConstants.LEFT);
	lSucursal.setFont(new Font("Arial", Font.BOLD, 18));
	lSucursal.setForeground(new Color(78,79,99,190));
        
        codSuc = new JTextField();
        nombSuc = new JTextField();
        dirSuc = new JTextField();
        telSuc = new JTextField();
        numTVen = new JTextField();
        
        cAccionesBoton=new JComboBox();
        cAccionesBoton.addItem("Crear");
        cAccionesBoton.addItem("Modificar");
        cAccionesBoton.addItem("Eliminar");
        cAccionesBoton.addActionListener(this);
                
	bConsultarSuc = new JButton("busq");
	configurarBoton(bConsultarSuc);
	bCrear = new JButton("Crear");
	configurarBoton(bCrear);
	bModificar = new JButton("Modificar");
	configurarBoton(bModificar);
	bEliminar = new JButton("Eliminar");
	configurarBoton(bEliminar);
        
        habilitarDisponibInicial();
    }
    
    private void configurarBoton(JButton boton) {
        boton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        boton.setOpaque(false);
	boton.setBorderPainted(false);
	boton.setBackground(new Color(0,0,0,0));
	boton.addActionListener(this);
    }
    
    private void habilitarDisponibInicial(){
        
                codSuc.setEditable(true);
                nombSuc.setEditable(false);
                dirSuc.setEditable(false);
                telSuc.setEditable(false);
                numTVen.setEditable(false);
                
                bConsultarSuc.setEnabled(true);
                bCrear.setEnabled(false);
                bModificar.setEnabled(false);
                bEliminar.setEnabled(false);
    }
    
    private void organizarComponentes() {
        /* ****************** Parte Norte Pestanna */
        
        JPanel pBotones = new JPanel(new GridLayout(1,3));
	pBotones.setBackground(new Color(0,0,0,0));
	pBotones.add(bCrear);
	pBotones.add(bModificar);
	pBotones.add(bEliminar);
        
        JPanel pEncab = new JPanel(new BorderLayout(0,10));//ec ef
	pEncab.setBorder(BorderFactory.createEmptyBorder(20,40,0,40));//n,w,s,e
	pEncab.setBackground(new Color(0,0,0,0));
	pEncab.add(lSucursal, BorderLayout.NORTH);
	pEncab.add(pBotones, BorderLayout.CENTER);
	pEncab.add(cAccionesBoton, BorderLayout.SOUTH);
        
        /* ****************** Parte Central Pestanna */
        
        JPanel pBusqueda = new JPanel(new BorderLayout());
	pBusqueda.setBackground(new Color(0,0,0,0));
        pBusqueda.add( codSuc, BorderLayout.CENTER );
        pBusqueda.add( bConsultarSuc, BorderLayout.EAST );
        
        JPanel pFormulario = new JPanel(new GridLayout(5,2,0,2));
	pFormulario.setBorder(BorderFactory.createEmptyBorder(0,40,20,40));//n,w,s,e
	pFormulario.setBackground(new Color(0,0,0,0));
        
	pFormulario.add( new JLabel( "Código:" ) );
        pFormulario.add( pBusqueda );
        pFormulario.add( new JLabel( "Nombre:" ));
        pFormulario.add( nombSuc );
        pFormulario.add( new JLabel( "Dirección:" ) );
        pFormulario.add( dirSuc );
	pFormulario.add( new JLabel( "Teléfono:" ) );
        pFormulario.add( telSuc );
	pFormulario.add( new JLabel( "Num. Cajas Registradoras:" ) );
        pFormulario.add( numTVen );
	
	/* ****************** General */
	setLayout(new BorderLayout(0,20)); // eC, eF
	this.add(pEncab, BorderLayout.NORTH);
	this.add(pFormulario, BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource()==bConsultarSuc){
            buscarSucursal();
        }
        //
        if(ae.getSource()==cAccionesBoton){
            habilitarAccionActual();
        }
        //
        if(ae.getSource()==bCrear){
            crearSucursal();
        }
        if(ae.getSource()==bModificar){
            modificarSucursal();
        }
        if(ae.getSource()==bEliminar){
            eliminarSucursal();
        }
    }
    
    private void buscarSucursal() {
        capturarDatos("consultar");        
    }
    
    private void habilitarAccionActual() {
        String accion = cAccionesBoton.getSelectedItem().toString();
                
        switch(accion){
            case "Crear":
                if(permisos[0]){
                    codSuc.setEditable(true);
                    nombSuc.setEditable(true);
                    dirSuc.setEditable(true);
                    telSuc.setEditable(true);
                    numTVen.setEditable(true);
                    
                    bConsultarSuc.setEnabled(true);
                    bCrear.setEnabled(true);
                    bModificar.setEnabled(false);
                    bEliminar.setEnabled(false);
                    break;
                }
                else{
                    habilitarDisponibInicial();
                }
            case "Modificar":
                if(permisos[1]){
                    codSuc.setEditable(true);
                    nombSuc.setEditable(true);
                    dirSuc.setEditable(true);
                    telSuc.setEditable(true);
                    numTVen.setEditable(true);
                    
                    bConsultarSuc.setEnabled(true);
                    bCrear.setEnabled(false);
                    bModificar.setEnabled(true);
                    bEliminar.setEnabled(false);
                    break;
                }
                else{
                    habilitarDisponibInicial();
                }
            case "Eliminar":
                if(permisos[2]){
                    codSuc.setEditable(true);
                    nombSuc.setEditable(false);
                    dirSuc.setEditable(false);
                    telSuc.setEditable(false);
                    numTVen.setEditable(false);
                    
                    bConsultarSuc.setEnabled(true);
                    bCrear.setEnabled(false);
                    bModificar.setEnabled(false);
                    bEliminar.setEnabled(true);
                    break;
                }
                else{
                    habilitarDisponibInicial();
                }
        }        
    }
    
    private ArrayList capturarDatos(String accion){
        datos.add(accion);
        datos.add("sucursal");
        
        datos.add(codSuc.getText());
        datos.add(nombSuc.getText());
        datos.add(dirSuc.getText());
        datos.add(telSuc.getText());
        datos.add(numTVen.getText());
        return datos;
    }
    
    private void crearSucursal() {
        capturarDatos("crear");
    }

    private void modificarSucursal() {
        capturarDatos("modificar");
    }

    private void eliminarSucursal() {
        capturarDatos("eliminar");
    }
}
